<?php
// 应用公共文件
use think\facade\Db;

function getFileUrl($path){
    return request()->domain().$path;
}

/**
 * 将客户端上传的数组转换为数据库对应使用的数组
 * @param $array
 * @return array
 */
function buildParam($array){
    $data = [];
    $request = request();
    foreach ($array as $server_item=>$client_item){
        $data[$server_item] = $request->param($client_item);
    }
    $data = array_filter($data,function ($item){
        return !is_null($item);
    });
    return $data;
}
/**
 * 写入执行信息记录
 */
function write_exe_log($use_time, $use_mem,$login_id = 0)
{
    if (empty(config('config.is_write_exe_log'))) {
        return false;
    }
    $source_url = empty($_SERVER["HTTP_REFERER"]) ? '未知来源' : $_SERVER["HTTP_REFERER"];
    $exe_log['ip'] = request()->ip();
    $exe_log['exe_url'] = request()->baseUrl();
    $exe_log['exe_time'] = $use_time;
    $exe_log['exe_memory'] = $use_mem;
    $exe_log['exe_os'] = \util\CommonUtil::instance()->get_os();
    $exe_log['source_url'] = $source_url;
    $exe_log['session_id'] = app()->session->getId();
    $exe_log['browser'] = \util\CommonUtil::instance()->browser_info();
    $exe_log['create_time'] = TIME_NOW;
    $exe_log['module'] = app()->http->getName();
    $exe_log['login_id'] = $login_id;
    $exe_log['header'] = json_encode(request()->header(), JSON_UNESCAPED_UNICODE);
    Db::name('exe_log')->insert($exe_log);
    return true;
}